<?php
defined("_ALLOW") or die ("Access denied");
?>
<?php
(!isset($_SESSION['username'])) or die ("Access denied");
?>
<?php
if (isset($_REQUEST['submit'])) {
	$email = $_REQUEST['emailgetpass'];
	$sidcheck = $_REQUEST['sid'];
	if ($sidcheck != $_SESSION['randomregister']) {
		echo "Mã xác nhận không hợp lệ";
	}
	else {
		unset($_SESSION['randomregister']);
		include_once("class/cmkuser.php");
		global $objmkuser;
		$objmkuser = $objmkuser->Doc($email);
		if ($objmkuser==null)
			echo "Email " . $email . "Chưa đăng ký.";
		else if ($objmkuser->active==0)
			echo "Tài khoản chưa được kích hoạt";
		else {
			$strrandom = "";
			$random = rand(10000,99999);
			$random = md5($random);
			$i=0;
			$randindex = 0;
			for ($i=0; $i<6; $i++) {
				$randindex = rand(0,31);
				$strrandom .= substr($random,$randindex,1);
			}
			$objmkuser->password = $strrandom;
			$objmkuser->ResetPassword();
			$message  = "<html>Password đã được reset";
			$message .= "<br />Email: " . $objmkuser->email;
			$message .= "<br />Password: " . $objmkuser->password;
			$message .= "</html>";
			$result = mysendmail("", $email, "New password in diaoc24g.vn", $message);
			if ($result)
			{
				$str = "<div style font-family:Tahoma; font-size: 12px; color:#990000; font-weight:bold; text-align:center>";
			 $str .= "Password đã được reset. Vui lòng kiểm tra email " . $email . ".<br />Cảm ơn bạn đã tham gia dioc24g.vn";
			 	$str .= "</div>";
				echo $str;
			 }
			else
			{
				$str = "<div style font-family:Tahoma; font-size: 12px; color:#990000; font-weight:bold; text-align:center>";
			 	$str .= "Không thể gửi thông tin đến " . $email . ". Vui lòng kiểm tra lại email. Xin cảm ơn bạn đã tham gia diaoc24g.vn";
				$str .= "</div>";
				echo $str;
			 }
		}
	}
}
else {
$strrandom = "";
$random = rand(10000,99999);
$random = md5($random);
$i=0;
$randindex = 0;
for ($i=0; $i<6; $i++) {
	$randindex = rand(0,31);
	$strrandom .= substr($random,$randindex,1);
}
$_SESSION['randomregister'] = $strrandom;
?>
<script language="javascript">
function checkemail (email) {
	if (email=="") {
		return false;
	}
	if (email.indexOf(" ")>-1) {
		return false;
	}
	if (email.indexOf("@")==-1) {
		return false;
	}
	var i=1;
	var slength=email.length;
	//Neu email la chuoi khong co dau .
	if (email.indexOf(".")==-1) {
		return false;
	}
	//Neu email la chuoi co 2 dau . gan nhau
	if (email.indexOf("..")!=-1) {
		return false;
	}
	//Neu email la chuoi co 2 dau @
	if (email.indexOf("@")!=email.lastIndexOf("@")) {
		return false;
	}
	//Neu email la chuoi co dau . cuoi cung
	if (email.lastIndexOf(".")==email.length-1) {
		return false;
	}
	//Neu email la chuoi co ky tu dau tien la so
	var strso="0123456789";
	if (strso.indexOf(email.charAt(0))!=-1) {
		return 0;
	}
	//Neu email la chuoi co ky tu khong thuoc cac ky tu sau:
	var str="abcdefghijklmnopqrstuvwxyz-@._0123456789";
	for (var j=0; j<email.length; j++) {
		if (str.indexOf(email.charAt(j))==-1) {
			return false;
		}
	}
	return true;
}
function checkgetpassword(form) {
	if (!checkemail(form.emailgetpass.value)) {
		form.emailgetpass.focus();
		return false;
	}
	if (form.sid.value=="") {
		form.sid.focus();
		return false;
	}
	return true;
}
</script>
<form id="frmregister" name="frmregister" method="post" action="index.php?module=com_usergetpassword" onSubmit="return checkgetpassword(this)">
  <table width="80%" border="0" align="center" cellpadding="0" cellspacing="2">
    <tr>
      <td height="28" align="center">&nbsp;</td>
      <td height="28" colspan="2"><strong>NH&#7852;N PASSWORD</strong></td>
    </tr>
    <tr>
      <td width="140">Email</td>
      <td colspan="2" ><input name="emailgetpass" type="text" id="emailgetpass" size="50" maxlength="50" onChange="this.value = trim(this.value); " /></td>
    </tr>
    <tr>
      <td>S&#7889; x&aacute;c nh&#7853;n</td>
      <td width="144"><input name="sid" type="text" id="sid" onChange="this.value=trim(this.value)" /></td>
      <td width="266"><img src="getimgxacnhan.php" /></td>
    </tr>
    <tr>
      <td> </td>
      <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
    </tr>
  </table>
</form>
<?php } ?>